layui.use(['form', 'table', 'treetable','admin'], function () {
    var form = layui.form;
    var $ = layui.jquery;
    var table = layui.table;
    var treetable = layui.treetable;
    var admin = layui.admin;
    var prefix = ctx + "system/dept";
    // 渲染表格
    layer.load(2);

    // 渲染表格
    var renderTable = function () {
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: 0,
            treeIdName: 'deptId',
            treePidName: 'parentId',
            elem: '#deptList',
            url: prefix + '/list',
            page: false,
            id: 'deptListTable',
            toolbar: '#toolbar',
            cols: [[
                {type: "radio", fixed: "left", width: 50},
                {field: 'deptName', minWidth: 100, title: '部门名称'},
                {field: 'orderNum', minWidth: 100, title: '排序'},
                {
                    field: 'status', width: 200, align: 'center', templet: function (d) {
                        if (d.status === "0") {
                            return '<span class="layui-badge layui-bg-green">正常</span>';
                        } else {
                            return '<span class="layui-badge layui-bg-cyan">停用</span>';
                        }
                    }, title: '状态'
                },
                {field: 'createTime', title: '创建时间', align: 'center', minWidth: 150},
                {fixed: 'right', title: '操作', toolbar: '#bar', align: 'center', width: 150}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });
    };

    renderTable();

     expandFlag = false;
    //头工具栏事件
    table.on('toolbar(deptList)', function (obj) {
        var checkedNodeInfo; //选中节点的数据
        var checkStatus = table.checkStatus(obj.config.id); //获取选中行状态
        switch (obj.event) {
            case 'add':
                console.log("add");
                var data = checkStatus.data;  //获取选中行数据
                if(data.length==0 ){  //如果没有选择上级节点，则默认获取根节点数据
                    checkedNodeInfo = obj.config.data[0];
                }
                else{
                    checkedNodeInfo =   checkStatus.data[0];
                    console.log(checkedNodeInfo);
                }
                data =null;
                showAddOrEditModel(data,checkedNodeInfo);
                break;
            case 'edit':
               var  dataArr = checkStatus.data;
                if(dataArr.length > 0){
                    console.log("edit op");
                }
                else{
                    layer.msg("请选择一条数据!");
                }

                //修改数据
                break;
            case 'expandAllBtn':
                expandAllBtn(); //展开/折叠
                break;
        }
    });

    $('#btn-refresh').click(function () {
        renderTable();
    });

// tree表格树 展开/折叠
    function expandAllBtn(){
        if (expandFlag) {
            treetable.expandAll('#deptList');
            $("#expandAllBtn i").removeClass().addClass('layui-icon layui-icon-shrink-right'); //展开图标
        } else {
            $("#expandAllBtn i").removeClass().addClass('layui-icon layui-icon-spread-left'); //收缩图标
            treetable.foldAll('#deptList');
        }
       expandFlag = expandFlag ? false: true;
    }

    //新增或者修改部门信息的弹框
    function showAddOrEditModel(data,checkedNodeInfo) {
        admin.putTempData('t_checkNode', checkedNodeInfo);
        admin.putTempData('formOk', false);
        layer.open({
            type: 2,
            title: data ? '修改部门' : '添加部门',
            area: ['600px', '550px'],
            content: '/system/dept/editForm',
            end: function () {
                renderTable();  // 成功刷新表格
            }
        });
    }


});